package org.jeecg.modules.zhouAdmin.help.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.zhouAdmin.admin.fabeicCenter.mapper.MlccFabricInfoFirstMapper;
import org.jeecg.modules.zhouAdmin.admin.fabeicCenter.mapper.MySysUserMapper;
import org.jeecg.modules.zhouAdmin.admin.fabeicCenter.pojo.MlccFabricInfoFirst;
import org.jeecg.modules.zhouAdmin.admin.fabeicCenter.pojo.MySysUser;
import org.jeecg.modules.zhouAdmin.help.mapper.MyMlccCategoryMaterialTestinfoMapper;
import org.jeecg.modules.zhouAdmin.help.mapper.MyMlccStandardTestinfoMapper;
import org.jeecg.modules.zhouAdmin.help.pojo.MlccCategoryMaterialTestinfo;
import org.jeecg.modules.zhouAdmin.help.pojo.MlccStandardTestinfo;
import org.jeecg.modules.zhouAdmin.help.service.IExpoetService;
import org.jeecg.modules.zhoupackage.mapper.*;
import org.jeecg.modules.zhoupackage.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExpoetServiceImpl implements IExpoetService {

    @Autowired
    MyMlccStandardTestinfoMapper standardTestinfoMapper;

    @Autowired
    MyMlccCategoryMaterialTestinfoMapper materialTestinfoMapper;

    @Autowired
    MyMlccMaterialLibuInfoMapper libuInfoMapper;

    @Autowired
    MyMlccDilinniInfoMapper dilinniInfoMapper;

    @Autowired
    MyMlccButtonsInfoMapper buttonsInfoMapper;

    @Autowired
    MyMlccCategoryInfoMapper categoryInfoMapper;

    @Autowired
    MlccFabricInfoMapper fabricInfoMapper;

    @Autowired
    MySysUserMapper userMapper;

    @Autowired
    MlccFabricInfoFirstMapper firstMapper;

    @Autowired
    MySysUserMapper sysUserMapper;

    /**
     * 导出标准尺码
     *
     * @param categoryId 类别id
     */
    @Override
    public List<MlccStandardTestinfo> exportSize(String categoryId) {
        QueryWrapper<MlccStandardTestinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        return standardTestinfoMapper.selectList(wrapper);
    }

    /**
     * 标准码批量导入
     *
     * @param list       excel中的数据
     * @param categoryId 类别id
     */
    @Override
    public int importExcel(List<MlccStandardTestinfo> list, String categoryId) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        int result = 0;
        for (MlccStandardTestinfo sizeinfo : list) {
            QueryWrapper<MlccStandardTestinfo> wrapper = new QueryWrapper<>();
            wrapper.eq("standard_name", sizeinfo.getStandardName());
            MlccStandardTestinfo standardTestinfo = standardTestinfoMapper.selectOne(wrapper);
            if (standardTestinfo != null) {
                // 更新
                standardTestinfo.setCategoryId(categoryId).setCreateBy(user.getRealname()).setUpdateBy(user.getRealname())
                        .setCode(sizeinfo.getCode()).setNumbers(sizeinfo.getNumbers()).setRemark(sizeinfo.getRemark());
                result = standardTestinfoMapper.updateById(standardTestinfo);
            } else {
                // 插入
                sizeinfo.setCategoryId(categoryId).setCreateBy(user.getRealname()).setUpdateBy(user.getRealname());
                result = standardTestinfoMapper.insert(sizeinfo);
            }
        }
        return result;
    }

    /**
     * 导出里布
     *
     * @param categoryId 类别id
     */
    @Override
    public List<MlccMaterialLibuInfo> exportlibu(String categoryId) {
        ArrayList<MlccMaterialLibuInfo> libuInfos = new ArrayList<>();
        QueryWrapper<MlccCategoryMaterialTestinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId).eq("is_libu", 0);
        List<MlccCategoryMaterialTestinfo> list = materialTestinfoMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            for (MlccCategoryMaterialTestinfo testinfo : list) {
                libuInfos.add(libuInfoMapper.selectById(testinfo.getOtherId()));
            }
        }
        return libuInfos;
    }

    /**
     * 导出低领尼列表
     *
     * @param categoryId 类别id
     */
    @Override
    public List<MlccDilinniInfo> exportdi(String categoryId) {
        ArrayList<MlccDilinniInfo> diInfos = new ArrayList<>();
        QueryWrapper<MlccCategoryMaterialTestinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId).eq("is_libu", 1);
        List<MlccCategoryMaterialTestinfo> list = materialTestinfoMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            for (MlccCategoryMaterialTestinfo testinfo : list) {
                diInfos.add(dilinniInfoMapper.selectById(testinfo.getOtherId()));
            }
        }
        return diInfos;
    }

    /**
     * 导出纽扣列表
     *
     * @param categoryId 类别id
     */
    @Override
    public List<MlccButtonsInfo> exportbotton(String categoryId) {
        ArrayList<MlccButtonsInfo> buttons = new ArrayList<>();
        QueryWrapper<MlccCategoryMaterialTestinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId).eq("is_libu", 2);
        List<MlccCategoryMaterialTestinfo> list = materialTestinfoMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            for (MlccCategoryMaterialTestinfo testinfo : list) {
                MlccButtonsInfo info = buttonsInfoMapper.selectById(testinfo.getOtherId());
                if (info != null) {
                    MlccCategoryInfo categoryInfo = categoryInfoMapper.selectById(info.getCategoryId());
                    if (categoryInfo != null) {
                        info.setType(categoryInfo.getCategoryName());
                    }
                }
                buttons.add(info);
            }
        }
        return buttons;
    }

    /**
     * 导入里布
     *
     * @param list       excel中的数据
     * @param categoryId 类别id
     */
    @Override
    public int importlibu(List<MlccMaterialLibuInfo> list, String categoryId) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        int result = 0;
        for (MlccMaterialLibuInfo libuInfo : list) {
            if (libuInfo != null && libuInfo.getLibuCode() != null && !"".equals(libuInfo.getLibuCode())) {
                QueryWrapper<MlccMaterialLibuInfo> wrapper = new QueryWrapper<>();
                wrapper.eq("libu_code", libuInfo.getLibuCode());
                MlccMaterialLibuInfo info = libuInfoMapper.selectOne(wrapper);
                if (info != null) {
                    QueryWrapper<MlccCategoryMaterialTestinfo> wrapper1 = new QueryWrapper<>();
                    wrapper1.eq("is_libu", 0).eq("other_id", info.getId()).eq("category_id", categoryId);
                    int integer = materialTestinfoMapper.selectCount(wrapper1);
                    if (integer == 0) {
                        MlccCategoryMaterialTestinfo testinfo = new MlccCategoryMaterialTestinfo();
                        testinfo.setCategoryId(categoryId).setOtherId(info.getId() + "").setIsLibu(0)
                                .setCreateBy(user.getRealname()).setUpdateBy(user.getRealname());
                        result = materialTestinfoMapper.insert(testinfo);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 导入低领尼
     *
     * @param list       excel中的数据
     * @param categoryId 类别id
     */
    @Override
    public int importdi(List<MlccDilinniInfo> list, String categoryId) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        int result = 0;
        for (MlccDilinniInfo diInfo : list) {
            QueryWrapper<MlccDilinniInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("dilinni_number", diInfo.getDilinniNumber());
            MlccDilinniInfo info = dilinniInfoMapper.selectOne(wrapper);
            if (info != null) {
                QueryWrapper<MlccCategoryMaterialTestinfo> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("is_libu", 1).eq("other_id", info.getId()).eq("category_id", categoryId);
                int integer = materialTestinfoMapper.selectCount(wrapper1);
                if (integer == 0) {
                    MlccCategoryMaterialTestinfo testinfo = new MlccCategoryMaterialTestinfo();
                    testinfo.setCategoryId(categoryId).setOtherId(info.getId() + "").setIsLibu(1)
                            .setCreateBy(user.getRealname()).setUpdateBy(user.getRealname());
                    result = materialTestinfoMapper.insert(testinfo);
                }
            }
        }
        return result;
    }

    /**
     * 导入纽扣
     *
     * @param list       excel中的数据
     * @param categoryId 类别id
     */
    @Override
    public int importbutton(List<MlccButtonsInfo> list, String categoryId) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        int result = 0;
        for (MlccButtonsInfo button : list) {
            QueryWrapper<MlccButtonsInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("buttons_code", button.getButtonsCode());
            MlccButtonsInfo info = buttonsInfoMapper.selectOne(wrapper);
            if (info != null) {
                QueryWrapper<MlccCategoryMaterialTestinfo> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("is_libu", 2).eq("other_id", info.getId()).eq("category_id", categoryId);
                int integer = materialTestinfoMapper.selectCount(wrapper1);
                if (integer == 0) {
                    MlccCategoryMaterialTestinfo testinfo = new MlccCategoryMaterialTestinfo();
                    testinfo.setCategoryId(categoryId).setOtherId(info.getId() + "").setIsLibu(2)
                            .setCreateBy(user.getRealname()).setUpdateBy(user.getRealname());
                    result = materialTestinfoMapper.insert(testinfo);
                }
            }
        }
        return result;
    }

    /**
     * 导出面料列表
     *
     * @param groupid 面料组id
     * @param code    面料编号
     * @param gyscode 供应商编号
     * @param status  状态
     */
    @Override
    public List<MlccFabricInfo> exportfabric(String groupid, String code, String gyscode, Integer status) {
        QueryWrapper<MlccFabricInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupid);
        if (code != null && !"".equals(code)) {
            wrapper.like("fabric_code", code);
        }
        if (gyscode != null && !"".equals(gyscode)) {
            wrapper.like("gysfabriccode", gyscode);
        }
        if (status != null) {
            if (status == 1) {
                wrapper.in("status", 0, 2);
            } else {
                wrapper.eq("status", status);
            }
        } else {
            wrapper.in("status", 0, 2);
        }
        List<MlccFabricInfo> list = fabricInfoMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            for (MlccFabricInfo fabricInfo : list) {
                MySysUser user = userMapper.selectById(fabricInfo.getSysuserId());
                if (user != null) {
                    fabricInfo.setGysname(user.getRealname());
                }
            }
        }
        return list;
    }

    /**
     * 导出主推面料列表
     */
    @Override
    public List<MlccFabricInfoFirst> exportFirstFabric() {
        QueryWrapper<MlccFabricInfoFirst> wrapper = new QueryWrapper<>();
        wrapper.gt("fabric_price", 0).in("status", 0, 2);
        List<MlccFabricInfoFirst> infoFirsts = firstMapper.selectList(wrapper);
        if (infoFirsts != null && infoFirsts.size() > 0) {
            for (MlccFabricInfoFirst first : infoFirsts) {
                MySysUser sysUser = sysUserMapper.selectById(first.getSysuserId());
                if (sysUser != null) {
                    first.setGysname(sysUser.getRealname());
                }
            }
        }
        return infoFirsts;
    }
}
